package com.supermap;

import com.supermap.client.NettyClient;
import com.supermap.crypto.CryproType;
import com.supermap.crypto.CryptoUtils;
import com.supermap.ipfs.IpfsClient;
import com.supermap.protocol.RpcRequest;
import io.netty.channel.ChannelFuture;
import org.junit.Test;

import java.util.UUID;
import java.util.Vector;

/**
 * @author liqs
 * @version 1.0
 * @date 2021/4/6 11:16
 */
public class ClientTestDemo {

    private IpfsClient ipfsClient = new IpfsClient("172.16.168.10", "5002");
    private String ipfsbackupIp = "172.16.168.10";
    private String ipfsbackupPort = "5003";

    @Test
    public void IpfsServiceClientTest() throws Exception {

        /**
         * 定义是否使用加密 CryproType.getNOCRY() || CryproType.getRSA()
         */
//        String crypto = CryproType.getNOCRY();
        CryproType crypto = CryproType.RSA;

        /**
         * 启动client服务
         */
//        NettyClient client = new NettyClient("127.0.0.1", 8080, crypto);
        NettyClient client = new NettyClient("172.16.168.10", 50011, crypto);
        client.start();
        //channel = client.getChannel();

        // （单线程）ipfsClient 节点上传数据，然后备份到 ipfsbackupIp:ipfsbackupPort
        for (int i = 0; i < 10; i++) {
            //  (1)文件上传到 ipfs 返回 hashId
            String hashId = ipfsClient.upBytes(UUID.randomUUID().toString().getBytes());
            // （2）构成 RpcRequest 后发送给服务端
            RpcRequest request = new RpcRequest();
            request.setInfoPurposes("client.message");
            request.setHashId(client.encrypt(hashId));
            request.setIpfsIp(ipfsbackupIp);
            request.setIpfsPort(ipfsbackupPort);
            request.setPublicKey(CryptoUtils.RSAPubToString(client.getClientPublicKey()));
            ChannelFuture channelFuture = client.writeAndFlush(request);
            if (channelFuture == null) {
                System.out.println("重连失败");
            }
        }

//        executeThread(client,8, 10);

        /**
         * tips: 必须加上（发送标志给服务端，然后客户端接收标志识别，最后客户端主动关闭客户端连接）
         */
        client.shutDownClient();
    }

    /**
     * @param threadCount 线程数
     * @param sigle       每个线程执行个数
     * @throws InterruptedException
     */
    void executeThread(NettyClient client, int threadCount, int sigle) throws InterruptedException {
        Vector<Thread> threads = new Vector<>();
        for (int i = 0; i < threadCount; i++) {
            IpfsThreadRead ipfsThreadRead = new IpfsThreadRead(client, ipfsClient, sigle);
            Thread thread = new Thread(ipfsThreadRead);
            threads.add(thread);
            thread.start();
        }
        // 保证线程执行完
        for (Thread t : threads) {
            t.join();
        }
    }

    class IpfsThreadRead implements Runnable {

        private IpfsClient ipfsClientthread;
        private int clientsigle;
        NettyClient client;

        public IpfsThreadRead(NettyClient client, IpfsClient ipfsClient, int sigle) {
            ipfsClientthread = ipfsClient;
            clientsigle = sigle;
            this.client = client;
        }

        @Override
        public void run() {
            for (int i = 0; i < clientsigle; i++) {
                //  (1)文件上传到 ipfs 返回 hashId
                String hashId = ipfsClient.upBytes(UUID.randomUUID().toString().getBytes());
                // （2）构成 RpcRequest 后发送给服务端
                RpcRequest request = new RpcRequest();
                request.setInfoPurposes("client.message");
                request.setHashId(client.encrypt(hashId));
                long end1 = System.currentTimeMillis();
                request.setIpfsIp(ipfsbackupIp);
                request.setIpfsPort(ipfsbackupPort);
                request.setPublicKey(CryptoUtils.RSAPubToString(client.getClientPublicKey()));
                client.writeAndFlush(request);
            }
        }

    }

}
